package com.xindaoapp.happypet.utils;

import java.util.List;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.BitmapDrawable;
import android.util.Pair;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

import com.alibaba.fastjson.JSON;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.ClubInfo;
import com.xindaoapp.happypet.bean.ClubInfo.Club;

public class PopupWindowManager {
    private PopupWindow popupWindow;

    public void getchoiceChannelList(final Context context, View parentView,
            final IRequest<Club> iRequest) {
        if (null == popupWindow) {
            // 实例化PopupWindow
            final View view =
                    LayoutInflater.from(context).inflate(R.layout.popuwindow_choice_channel, null);
            final WheelView country = (WheelView) view.findViewById(R.id.country);
            final String countries[] =
                    new String[] {"汪星人俱乐部", "喵星人俱乐部", "水族俱乐部", "小宠俱乐部", "火热", "地方", "综合"};
            country.setVisibleItems(5);
            ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<String>(context, countries);
            adapter.setTextSize(20);
            country.setViewAdapter(adapter);
            final WheelView city = (WheelView) view.findViewById(R.id.city);
            city.setVisibleItems(5);

            final Pair<ClubInfo[], String[][]> cities = getCities(context);

            view.findViewById(R.id.popuwindow_cancel).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    popupWindow.dismiss();
                }
            });
            view.findViewById(R.id.popuwindow_sure).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    popupWindow.dismiss();
                    iRequest.request(cities.first[country.getCurrentItem()].getArray().get(
                            city.getCurrentItem()));
                }
            });

            country.addChangingListener(new OnWheelChangedListener() {
                @Override
                public void onChanged(WheelView wheel, int oldValue, int newValue) {
                    ArrayWheelAdapter<String> adapter =
                            new ArrayWheelAdapter<String>(context, cities.second[newValue]);
                    adapter.setTextSize(20);
                    city.setViewAdapter(adapter);
                    city.setCurrentItem(cities.second[newValue].length / 2);
                }
            });
            country.setCurrentItem(2);
            popupWindow =
                    new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            popupWindow.setAnimationStyle(R.style.PopupAnimation);
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setOutsideTouchable(true);
            popupWindow.setFocusable(true);
        }
        popupWindow.showAtLocation(parentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL,
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); // 设置layout在PopupWindow中显示的位置
    }

    private Pair<ClubInfo[], String[][]> getCities(Context context) {
        SharedPreferences sp = context.getSharedPreferences("menuconfig", Context.MODE_PRIVATE);
        ClubInfo info1 = JSON.parseObject(sp.getString("wxr", ""), ClubInfo.class);
        List<Club> wxr = info1.getArray();
        ClubInfo info2 = JSON.parseObject(sp.getString("mxr", ""), ClubInfo.class);
        List<Club> mxr = info2.getArray();
        ClubInfo info3 = JSON.parseObject(sp.getString("sz", ""), ClubInfo.class);
        List<Club> sz = info3.getArray();
        ClubInfo info4 = JSON.parseObject(sp.getString("xc", ""), ClubInfo.class);
        List<Club> xc = info4.getArray();
        ClubInfo info5 = JSON.parseObject(sp.getString("hr", ""), ClubInfo.class);
        List<Club> hr = info5.getArray();
        ClubInfo info6 = JSON.parseObject(sp.getString("location", ""), ClubInfo.class);
        List<Club> location = info6.getArray();
        ClubInfo info7 = JSON.parseObject(sp.getString("all", ""), ClubInfo.class);
        List<Club> all = info7.getArray();

        String[] strArr1 = new String[wxr.size()];
        for (int i = 0; i < strArr1.length; i++) {
            strArr1[i] = wxr.get(i).getName();
        }

        String[] strArr2 = new String[mxr.size()];
        for (int i = 0; i < strArr2.length; i++) {
            strArr2[i] = mxr.get(i).getName();
        }

        String[] strArr3 = new String[sz.size()];
        for (int i = 0; i < strArr3.length; i++) {
            strArr3[i] = sz.get(i).getName();
        }

        String[] strArr4 = new String[xc.size()];
        for (int i = 0; i < strArr4.length; i++) {
            strArr4[i] = xc.get(i).getName();
        }
        String[] strArr5 = new String[hr.size()];
        for (int i = 0; i < strArr5.length; i++) {
            strArr5[i] = hr.get(i).getName();
        }
        String[] strArr6 = new String[location.size()];
        for (int i = 0; i < strArr6.length; i++) {
            strArr6[i] = location.get(i).getName();
        }
        String[] strArr7 = new String[all.size()];
        for (int i = 0; i < strArr7.length; i++) {
            strArr7[i] = all.get(i).getName();
        }

        ClubInfo[] clubInfos = new ClubInfo[] {info1, info2, info3, info4, info5, info6, info7,};
        return new Pair<ClubInfo[], String[][]>(clubInfos, new String[][] {strArr1, strArr2,
                strArr3, strArr4, strArr5, strArr6, strArr7});
    }
}
